package jia.temp;

import java.awt.Color;
import java.net.MalformedURLException;
import jia.game.GenericFlags;
import jia.core.GameStateManager;
import jia.core.InputProcessor;
import jia.core.UiManager;
import jia.core.event.KeypressUiEvent;
import jia.core.processors.BasicInputProcessor;
import jia.core.state.BasicGameStateManager;
import jia.core.state.BasicUiManager;
import jia.core.state.PlayerState;
import jia.game.TestGame;
import org.apache.log4j.Logger;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class ViewportTest extends JFrame{
    private static final Logger log = Logger.getLogger(ViewportTest.class);
    private GameStateManager mgr;
    private InputProcessor inputProc;
    private UiManager uiManager;
    
    /**
     * @param args
     */
    public static void main(String[] args){
        new ViewportTest().setVisible(true);
    }
    
    /**
     */
    public ViewportTest(){
        //create a new Game Manager.  This is the brain
        mgr = new BasicGameStateManager();
        
        //access the players current state (where, when, etc)
        PlayerState ps = mgr.getPlayerState();
        
        //create a uiManager and an input processor
        uiManager = new BasicUiManager(mgr);
        inputProc = new BasicInputProcessor(mgr);
        
        //uiManager.registerGameState(mgr);
        uiManager.registerInputProcessor(inputProc);
        mgr.registerUiManager(uiManager);
        TestGame testGame = null;
        try {
            testGame = new TestGame();
        } catch (MalformedURLException ex) {
            if (GenericFlags.errorMessages){
                log.error(ex.getMessage());
            }
        }
        //TestDbGame testGame = new TestDbGame();
        
        //this should be the title of the module
        setTitle(testGame.getTestModule().getName());
        setBackground(new java.awt.Color(62, 43, 5));
        
        
        //make sure the program halts when it is closed.
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        //fill the screen with the UI, and set the size of the interface and make sure it cant be resized.
        getContentPane().add(uiManager.getUiPanel());
        setSize(850, 580);
        setResizable(true);
        
        //modify the player's state with the module loaded from the testgame.
        ps.setCurrentModule(testGame.getTestModule());
        
        //set the user's starting position based on what the module has defined.
        ps.setPosition(ps.getCurrentModule().getDefaultEntryPoint().getEntryPosition());
        ps.setFacing(ps.getCurrentModule().getDefaultEntryPoint().getEntryFacing());
        
        //create a key listener so that when the arrow keys are used, the player can move around.
        addKeyListener(new MyKeyAdapter());
        
        //now that the player has entered the world, check to see if there is an event that needs
        //to trigger and execute it.
        mgr.notifyPlayerStateChanged();
        
        if(GenericFlags.debugMessages) {
            log.debug("Game state initialized.");
        }
    }
    
    
    /**
     * Inner class that handles the listening of keypresses.  It is possible that
     * this can be refactored to it's own class at a later date.
     */
    class MyKeyAdapter extends KeyAdapter{
        public void keyPressed(KeyEvent evt){
            int keyCode = evt.getKeyCode();
            if(keyCode == KeyEvent.VK_ESCAPE) {
                dispose();
                return;
            }
            if(keyCode == KeyEvent.VK_F2) {
                ((BasicUiManager) uiManager).setAreaViewState();
            }
            if(keyCode == KeyEvent.VK_F3) {
                ((BasicUiManager) uiManager).setStandardViewState();
            }
            KeypressUiEvent kpEvt = new KeypressUiEvent(keyCode);
            inputProc.processUiEvent(kpEvt);
        }
    }
}
